//app.js
var GGsysInfo = wx.getSystemInfoSync()//获取设备信息
var ext_conf = wx.getExtConfigSync ? wx.getExtConfigSync() : {};  //获取开放平台设置功能
// //////console.log(GGsysInfo);
////console.log("GGsysInfo 的 platform是"+GGsysInfo.platform);
//GGsysInfo的platform代表平台例如 开发者平台

//if (true || GGsysInfo.platform == 'devtools') {
if (GGsysInfo.platform == 'devtools') {
  // wx.showModal({
  //     title: 'who==',
  //     content: '=========== 获取本地的 ext_conf  ==============='
  // });
  ext_conf = {
    //"who":19476,
    //"who":13141,
    //"who": 14127,
    // "who": 395,
    //"who":2135,  //有后台账号
    //"who":7160,//商品属性  订单显示没问题
    //"who":15973,
    //"who":1988,
    //"who": 19437,//烤肉饭
    //"who": 8749,
    //"who":18597,
    //"who": 6,
    // "shopType": 0,
    // "cartCollect": 0,
    "apihost": "https://apissl.xinweiyun.com"
    //"apihost":"https://safe.yundianba.cn",
    // "template_id": 9
  }
//}
} else {
  ////////console.log('=========== 获取 微信服务器上的 ext_conf  ===============');
  // wx.showModal({
  //     title: 'who==',
  //     content: '=========== 获取 微信服务器上的 ext_conf  ==============='
  // });
}
if (!ext_conf.who) {
  // wx.showModal({
  //     title: '提示',
  //     content: '无法获取who值，请联系管理员'
  // })
}

//    opentableapp
App({
  data: {},
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now());
    //wx.setStorageSync('logs', logs)
    this.getShopconf();
    // //////console.log("getShopconf 返回" + this.getShopconf);
    this.getUserInfo();
  },
  globalData: {
    userInfo: '',
    who: ext_conf.who,
    commonURL: ext_conf.apihost + "/weixin/index.php/",
    // commonURL: "https://apissl.xinweiyun.com/weixin/index.php/",
    appData: {},
  },


  //获取用户信息
    getUserInfo: function () {
        console.log('获取用户信息');
        var that = this;
        /**
         * 获取用户个人信息
         */
        wx.login({
            success: function (res) {
                console.log(res);
                //获取openid
                console.log('获取用户个人信息');
                console.log('res.code');
                console.log( res.code);
                var httpObj = {
                    url:"wxApp/getSession",
                    method: "POST",
                    data: {
                        who: that.globalData.who,
                        code: res.code,
                    },
                    success:function (res) {
                        console.log(res);
                        console.log('app.js openid');
                        console.log(res);
                        console.log('openid-------------------------------------------------------');
                        console.log(res.data.openid);
                        that.globalData.openid = res.data.openid;
                        console.log('app.globalData.openid');
                        console.log(that.globalData.openid);
                        var loginHttpObj = {
                            url: "userother/login",
                            data: {
                                who: that.globalData.who,
                                openid: res.data.openid,
                                username:that.globalData.userInfo.nickName,
                                headimgsrc:that.globalData.userInfo.avatarUrl
                            },
                            success: function (res) {
                                console.log('userid---------------------------------------------------');
                                console.log(res.data.userid);
                                that.globalData.userid = res.data.userid;
                                that.getaddress(res.data.userid);
                            }
                        }
                        that.http(loginHttpObj);

                        //获取微信返回的个人信息
                        wx.getUserInfo({
                            success: function (res) {
                                console.log("获取用户信息成功--------------------------");
                                that.globalData.userInfo = res.userInfo;
                                ////console.log(that.globalData.userInfo);
                                /**
                                 * 获取微信用户登录code
                                 */
                                console.log("app js");
                                console.log(that.globalData.userInfo.avatarUrl);

                            },
                            fail:function(err){
                                console.log('获取用户个人信息失败----------------------');
                                console.log(err);
                            }
                        });
                    }
                };
                that.http(httpObj);
            }
        })

        console.log('获取用户信息结束');
    },



  //获取默认地址
  getaddress: function (userid,success) {
    var that = this;
    //取地址信息
    wx.request({
      url: that.globalData.commonURL +  'userother/addresslist',
      data: {
        userid: userid
      },
      method: 'POST',
      success: function (res) {
        //////console.log("获取地址");
        //////console.log(res);
        if (res.data.status == 0) {
          that.globalData.address = null;
        }else{
          that.globalData.address = res.data.address_list[0];
        }
        if(success!=undefined){
          //////console.log("调用success");
          success()
        }
      },
      header: {
        // 'content-type': 'application/json'
        'content-type': 'application/x-www-form-urlencoded'
      },
      fail: function (err) {
      }
    })

  },


  /**
   * 获取店铺信息添加到globalData
   * @param callback 回调
   */
  getShopconf: function (callback) {
    var that = this;
    wx.request({
      url: this.globalData.commonURL + "XwyAppPage/shopconf",
      data: {
        who: that.globalData.who
      },
      success: function (res) {
        that.globalData.appData = res.data.shop
        callback && callback();
      }
    })
  },


  /**
   * 设置导航栏标题
   * @param title 标题名称
   */
  setNavigationBarTitle: function (title) {
    wx.showNavigationBarLoading();
    wx.setNavigationBarTitle({
      title: title || ''
    });
    wx.hideNavigationBarLoading()
  },
  /**
   * 发起http请求
   * @param obj  wx.request的参数对象
   */
  http: function (obj = {}) {
    var that = this;
    let data= obj.data || {};
    data.from='wxapp';
    wx.request({
      url: that.globalData.commonURL + obj.url,
      data:data,
      method: obj.method || 'GET',
      success: function (res) {
        obj.success && obj.success(res);
      },
      header: obj.header || {
        // 'content-type': 'application/json'
        'content-type': 'application/x-www-form-urlencoded'
      },
      fail: function (erorr) {
        obj.fail && obj.fail(erorr);
        console.log(erorr);
      }
    })
  },
  title: [],
  imgUrls: [],
  author: [],
  date: [],
  url: [],
  requestUrl: "top",
  cssActive: 0,
  page: 0,


})
